﻿#include "widget.h"
#include <QFile>
#include <QIcon>
#include <QApplication>
#include <QMessageBox>
#include <QSharedMemory>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //设置QSS
    QFile m_styleFile(QString(":/QSS/stylesheet.qss"));
    m_styleFile.open(QIODevice::ReadOnly);
    a.setStyleSheet(QString(m_styleFile.readAll()));
    a.setWindowIcon(QIcon(":/ICO/favicon512.ico"));

    //创建单实例
    auto shareMen = new QSharedMemory(QString("SingleInstanceIdentify"));
    if(shareMen->attach(QSharedMemory::ReadOnly))
    {
        shareMen->detach();
        shareMen->attach(QSharedMemory::ReadOnly);
    }
    if(!shareMen->create(1))
    {
        QMessageBox::warning(nullptr,"警告","已经有一个实例在运行","确   定");
        shareMen->deleteLater();
        return 0;
    }

    //创建窗口
    Widget w;
    w.show();
    a.exec();

    //程序退出后释放共享内存
    if(shareMen->isAttached())
    {
        shareMen->detach();
        shareMen->deleteLater();
    }
    return 0;
}
